package com.kingint.web.util.thirdPartyInterface;


import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Map;

@Component
public class HttpUtils {
    private static CloseableHttpClient httpClient;

    static {
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(100);
        cm.setDefaultMaxPerRoute(20);
        cm.setDefaultMaxPerRoute(50);
        httpClient = HttpClients.custom().setConnectionManager(cm).build();
    }


    public static String getPostInfo(String payUrl, String jsonString) {
        CloseableHttpResponse response = null;
        BufferedReader in = null;
        String result = "";
        try {
            HttpPost httpPost = new HttpPost(payUrl);
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000).setConnectionRequestTimeout(3000).setSocketTimeout(3000).build();
            httpPost.setConfig(requestConfig);
            httpPost.setConfig(requestConfig);
            httpPost.addHeader("Content-type", "application/x-www-form-urlencoded");
            httpPost.setEntity(new StringEntity(jsonString, Charset.forName("UTF-8")));
            response = httpClient.execute(httpPost);
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                System.out.println(line + NL);
                sb.append(line + NL);
            }
            in.close();
            result = sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != response) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }


    public static String getPost(String payUrl, String jsonString) {
        CloseableHttpResponse response = null;
        BufferedReader in = null;
        String result = "";
        try {
            HttpPost httpPost = new HttpPost(payUrl);
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(2000).setConnectionRequestTimeout(2000).setSocketTimeout(2000).build();
            httpPost.setConfig(requestConfig);
            httpPost.setConfig(requestConfig);
            httpPost.addHeader("Content-type", "application/json");
            httpPost.setEntity(new StringEntity(jsonString, Charset.forName("UTF-8")));
            response = httpClient.execute(httpPost);
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                System.out.println(line + NL);
                sb.append(line + NL);
            }
            in.close();
            result = sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != response) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }


    /**
     * HTTPS GET请求
     * @param payUrl
     * @return
     */
    public static String HttpsGet(String payUrl){
        return HttpsGet(payUrl,null);
    }
    public static String HttpsGet(String payUrl, String jsonString){
        String response = null;
        try {
            HttpClient httpClient = new HttpClient(payUrl);
            if(jsonString != null){
                httpClient.setParameter((Map) JSONObject.parse(jsonString));
            }
            httpClient.setHttps(true);
            httpClient.get();
            response = httpClient.getContent();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

    /**
     * HTTPS POST请求
     * @param payUrl
     * @return
     */
    public static String HttpPost(String payUrl){
        return HttpsPost(payUrl,null,null);
    }
    public static String HttpsPost(String payUrl, String jsonString,String heard){
        String response = null;
        try {
            HttpClient httpClient = new HttpClient(payUrl);
            if(jsonString != null){
                httpClient.setParameter((Map) JSONObject.parse(jsonString));
            }
            if(heard != null){
                httpClient.setHeader((Map) JSONObject.parse(heard));
            }
            httpClient.setHttps(true);
            httpClient.post();
            response = httpClient.getContent();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }
}
